package com.kkb.controller;

import com.kkb.pojo.ResultVO;
import com.kkb.pojo.User;
import com.kkb.service.UserService;
import com.kkb.util.VerifyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("admin")
public class AdminController {
    @Resource
    private UserService userService;

    @RequestMapping("createImg")
    //@Log(description="图形验证码生成")
    public void createImg(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
            response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            VerifyUtil randomValidateCode = new VerifyUtil();
            randomValidateCode.getRandCode(request, response);//输出验证码图片
            //将生成的随机验证码存放到redis中
            //redisService.setForValue("RANDOMVALIDATECODEKEY",(String)request.getSession().getAttribute("RANDOMREDISKEY "),Long.parseLong("60000"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "login",method = RequestMethod.POST)
    @ResponseBody
    public ResultVO login(String userName, String password,String verifyCode,HttpServletRequest request) {
       /* if (userName == null && password == null) {
            return new ResultVO(500);
        }
        System.out.println("userName:" + userName + ",password:" + password);*/
        User user = userService.login(userName, password);
        ResultVO resultVO = new ResultVO();
        if (user!=null) {
            if(user.getUserState()==1){
                return new ResultVO(500,"抱歉，您的账号已被禁用，请联系管理员");
            }
            HttpSession session = request.getSession();
            String randomKey = (String) session.getAttribute("randomKey");
            if (randomKey == null) {
                resultVO.setMsg("验证码过期,请重新获取验证码！");
            } else if (verifyCode.equals(randomKey)) {
                session.invalidate();
                HttpSession session1 = request.getSession();
                user.setUserPassword("");
                session1.setAttribute("user",user);
                resultVO.setCode(200);
            }else {
                resultVO.setMsg("验证码有误！请重新输入");
            }
        }else {
            resultVO.setCode(401);
            resultVO.setMsg("登录失败,用户名或密码有误");
        }
        return resultVO;
    }
    @RequestMapping(value = "pwd",method = RequestMethod.POST)
    @ResponseBody
    public ResultVO changePwd(String password,String password1,String password2,HttpServletRequest request){
        HttpSession session = request.getSession();
        User user1 = (User) session.getAttribute("user");
        List<User> userList = userService.queryByUserLoginName(user1.getUserLoginName());
        //System.out.println("password:"+password+"password1:"+password1+"password2:"+password2);
        if(userList.size()==1){
            User user = userList.get(0);
            String userPassword = user.getUserPassword();
            if(password.equals(userPassword)){
                if(password1.equals(password)){
                    return new ResultVO(500,"新密码不能和旧密码一样");
                }
                if(password1.equals(password2)){
                    user.setUserPassword(password1);
                    int i = userService.updateUser(user);
                    if(i==1){
                        session.invalidate();
                        return new ResultVO(200,"修改成功");
                    }
                }else {
                    return new ResultVO(500,"两次密码输入不一致");
                }
            }else {
                return new ResultVO(500,"原密码错误");
            }
        }
        return new ResultVO(500,"密码修改失败");
    }

    @RequestMapping(value = "userInfo", method = RequestMethod.GET)
    @ResponseBody
    public ResultVO<User> getUserTrueName(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if(user!=null){
            return new ResultVO<>(200, user);
        }
        return new ResultVO<>(500);
    }
    @RequestMapping(value = "logout", method = RequestMethod.GET)
    @ResponseBody
    public ResultVO<User> logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.invalidate();
        return new ResultVO<>(200);
    }
   /* @RequestMapping(value = "verifyCode")
    @ResponseBody
    public ResultVO verifyCode(String verifyCode,HttpServletRequest request){
        String randomKey = (String) request.getSession().getAttribute("randomKey");
        if(randomKey.equals(verifyCode)){
            return new ResultVO(200,"验证码正确");
        }else {
            return new ResultVO(500,"验证码错误");
        }
    }*/
}
